<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="代办日期" prop="stayTimer">
        <el-date-picker
          clearable
          v-model="queryParams.stayTimer"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择代办日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="人才名称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入人才名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="代办类型" prop="handleType">
        <el-select v-model="queryParams.handleType" placeholder="请选择代办类型" clearable>
          <el-option
            v-for="dict in dict.type.handle_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="代办事项" prop="matter">
        <el-input
          v-model="queryParams.matter"
          placeholder="请输入代办事项"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="服务状态" prop="serveStatus">
        <el-select v-model="queryParams.serveStatus" placeholder="请选择服务状态" clearable>
          <el-option
            v-for="dict in dict.type.serve_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="受理备注" prop="acceptRemark">
        <el-input
          v-model="queryParams.acceptRemark"
          placeholder="请输入受理备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="人才类型" prop="eliteType">
        <el-select v-model="queryParams.eliteType" placeholder="请选择人才类型" clearable>
          <el-option
            v-for="dict in dict.type.elite_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['serve:handle:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['serve:handle:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['serve:handle:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['serve:handle:export']"
        >
          导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="handleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="代办日期" align="center" prop="stayTimer" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.stayTimer, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="人才名称" align="center" prop="nickName" />
      <el-table-column label="联系方式" align="center" prop="phonenumber" />
      <el-table-column label="代办类型" align="center" prop="handleType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.handle_type" :value="scope.row.handleType" />
        </template>
      </el-table-column>
      <el-table-column label="代办事项" align="center" prop="matter" />
      <!-- <el-table-column label="代办材料" align="center" prop="materials" /> -->
      <el-table-column label="服务状态" align="center" prop="serveStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.serve_state" :value="scope.row.serveStatus" />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark">
        <template slot-scope="scope">
          <div class="u-line-2" :title="scope.row.remark">{{ scope.row.remark }}</div>
        </template>
      </el-table-column>
      <el-table-column label="受理备注" align="center" prop="acceptRemark">
        <template slot-scope="scope">
          <div class="u-line-2" :title="scope.row.acceptRemark">{{ scope.row.acceptRemark }}</div>
        </template>
      </el-table-column>
      <el-table-column label="人才类型" align="center" prop="eliteType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.elite_type" :value="scope.row.eliteType" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleUpdate(scope.row, 2)"
            v-hasPermi="['serve:handle:edit']"
          >
            查看
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row, 3)"
            v-if="scope.row.serveStatus == 0"
            v-hasPermi="['serve:handle:edit']"
          >
            审核
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row, 4)"
            v-if="scope.row.serveStatus == 1"
            v-hasPermi="['serve:guarantee:accept']"
          >
            提交
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleSuccess(scope.row.id, 'Y')"
            v-if="scope.row.showFinish"
            v-hasPermi="['serve:guarantee:finish']"
          >
            办结
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改代办对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="代办日期" prop="stayTimer">
          <el-date-picker
            clearable
            v-model="form.stayTimer"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择代办日期"
            disabled
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="人才名称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入人才名称" disabled />
        </el-form-item>
        <el-form-item label="人才类型" prop="eliteType">
          <el-select v-model="form.eliteType" placeholder="请选择人才类型" disabled>
            <el-option
              v-for="dict in dict.type.elite_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="代办类型" prop="handleType">
          <el-select v-model="form.handleType" placeholder="请选择代办类型" disabled>
            <el-option
              v-for="dict in dict.type.handle_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="代办事项" prop="matter">
          <el-input v-model="form.matter" placeholder="请输入代办事项" disabled />
        </el-form-item>
        <el-form-item label="代办材料" prop="materials">
          <file-upload v-model="form.materials" :dis="true" />
        </el-form-item>
        <!-- <el-form-item label="服务状态" prop="serveStatus">
          <el-select v-model="form.serveStatus" placeholder="请选择服务状态">
            <el-option
              v-for="dict in dict.type.serve_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" disabled />
        </el-form-item>
        <el-form-item
          label="证明文件"
          prop="documentation"
          v-hasPermi="['serve:guarantee:accept']"
          v-if="form.serveStatus == '1'"
        >
          <file-upload
            v-model="form.documentation"
            :dis="form.serveStatus != '1' || openType != 4"
            :fileType="[
              'doc',
              'xls',
              'ppt',
              'txt',
              'pdf',
              'png',
              'jpg',
              'jpeg',
              'mp4',
              'avi',
              'wmv'
            ]"
          />
        </el-form-item>
        <el-form-item label="受理备注" prop="acceptRemark">
          <el-input
            v-model="form.acceptRemark"
            placeholder="请输入受理备注"
            :disabled="openType == 2 || form.serveStatus != 0"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <template v-if="form.serveStatus == 0 && openType == 3">
          <el-button type="primary" @click="submitForm(true)">受 理</el-button>
          <el-button type="danger" @click="submitForm(false)">驳 回</el-button>
        </template>
        <el-button
          v-if="form.serveStatus == 1 && openType == 4"
          type="primary"
          @click="submitForm(true)"
          v-hasPermi="['serve:guarantee:accept']"
        >
          提 交
        </el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listHandle, getHandle, delHandle, addHandle, updateHandle } from '@/api/serve/handle'
import { acceptHandle, handleFinish } from '@/api/serve/accept.js'

export default {
  name: 'Handle',
  dicts: ['handle_type', 'elite_type', 'serve_state', 'sys_yes_no'],
  data() {
    return {
      openType: 1,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 代办表格数据
      handleList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stayTimer: null,
        nickName: null,
        phonenumber: null,
        handleType: null,
        matter: null,
        materials: null,
        serveStatus: null,
        starRating: null,
        evaluate: null,
        acceptRemark: null,
        eliteType: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        acceptRemark: [{ required: true, trigger: 'blur', message: '请输入受理备注' }],
        documentation: [{ required: true, trigger: 'blur', message: '请上传证明文件' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    //办结
    handleSuccess(id, finish) {
      this.$modal
        .confirm(`是否办结此条记录？`)
        .then(() => {
          const params = {
            id,
            finish
          }
          handleFinish(params).then((res) => {
            this.$modal.msgSuccess('办结成功')
            this.getList()
          })
        })
        .catch(() => {})
    },
    /** 查询代办列表 */
    getList() {
      this.loading = true
      listHandle(this.queryParams).then((response) => {
        this.handleList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        stayTimer: null,
        nickName: null,
        phonenumber: null,
        handleType: null,
        matter: null,
        materials: null,
        serveStatus: null,
        starRating: null,
        evaluate: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        acceptRemark: null,
        eliteType: null,
        documentation: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.openType = 1
      this.title = '添加代办'
    },
    /** 修改按钮操作 */
    handleUpdate(row, openType) {
      this.reset()
      this.openType = openType
      const id = row.id || this.ids
      getHandle(id).then((response) => {
        this.form = response.data
        this.open = true
        if (response.data.materials) {
          this.form.materials = JSON.parse(response.data.materials)
        }
        this.title = openType == 2 ? '查看代办' : '审核代办'
      })
    },
    /** 提交按钮 */
    submitForm(accept) {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            if (this.openType == 4) {
              const params = {
                id: this.form.id,
                documentation: this.form.documentation
              }
              updateHandle(params).then((response) => {
                this.$modal.msgSuccess('提交成功')
                this.open = false
                this.getList()
              })
            } else {
              const params = {
                id: this.form.id,
                accept,
                acceptRemark: this.form.acceptRemark
              }
              acceptHandle(params).then((response) => {
                this.$modal.msgSuccess('提交成功')
                this.open = false
                this.getList()
              })
            }
          } else {
            addHandle(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal
        .confirm('是否确认删除代办编号为"' + ids + '"的数据项？')
        .then(function () {
          return delHandle(ids)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'serve/handle/export',
        {
          ...this.queryParams
        },
        `handle_${new Date().getTime()}.xlsx`
      )
    }
  }
}
</script>
